<?php
// 检查oa_admin_rule表结构的脚本

// 数据库连接配置
$host = '127.0.0.1';
$dbname = 'oa5';
$username = 'root';
$password = '123456';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // 查询表结构
    $sql = "DESCRIBE oa_admin_rule";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    
    $columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "oa_admin_rule 表结构:\n";
    echo str_repeat("-", 80) . "\n";
    printf("%-20s %-15s %-10s %-10s %-10s\n", "字段名", "类型", "是否为空", "键", "默认值");
    echo str_repeat("-", 80) . "\n";
    
    foreach ($columns as $column) {
        printf("%-20s %-15s %-10s %-10s %-10s\n", 
               $column['Field'], 
               $column['Type'], 
               $column['Null'], 
               $column['Key'], 
               $column['Default']);
    }
    
    // 检查是否存在mig_src字段
    $hasMigSrc = false;
    foreach ($columns as $column) {
        if ($column['Field'] === 'mig_src') {
            $hasMigSrc = true;
            break;
        }
    }
    
    echo "\n";
    if ($hasMigSrc) {
        echo "表中存在 mig_src 字段。\n";
    } else {
        echo "表中不存在 mig_src 字段，需要添加该字段。\n";
        
        // 添加mig_src字段
        $alterSql = "ALTER TABLE oa_admin_rule ADD COLUMN mig_src VARCHAR(255) NOT NULL DEFAULT '' COMMENT '迁移后的前端路由地址' AFTER src";
        $alterStmt = $pdo->prepare($alterSql);
        $alterStmt->execute();
        echo "已添加 mig_src 字段。\n";
    }
    
} catch(PDOException $e) {
    echo "错误: " . $e->getMessage() . "\n";
}